package com.dyzs.util;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseOperation {	
	SQLiteDatabase db=null;
	DatabaseHelper dbHelper=null;
	Context context=null;	
	public DatabaseOperation(Context contextt){
		context=contextt;
	}
 	public  void  insert(String table,ContentValues contentValue) throws Exception{ 		
 		try{
 			dbHelper = new DatabaseHelper(context);
 			db = dbHelper.getWritableDatabase();	
 			db.insert(table, null, contentValue); 			
 		}catch(Exception e){ 
 			throw new Exception("数据库操作失败");
 		}finally{
 			db.close();
 		}		
	}
 	
 	
 	public  void  update(String table,ContentValues contentValue,String selection,String[] selectionArgs ) throws Exception{ 		
 		try{
 			dbHelper = new DatabaseHelper(context);
 			db = dbHelper.getWritableDatabase();
 			db.update(table, contentValue,selection, selectionArgs);  			
 		}catch(Exception e){ 
 			throw new Exception("数据库操作失败");
 		}finally{
 			db.close();
 		}		
	}
 	
 	
 	
 	
 	public  void  update(String sql) throws Exception{ 		
 		try{
 			dbHelper = new DatabaseHelper(context);
 			db = dbHelper.getWritableDatabase();
 			db.execSQL(sql);  			
 		}catch(Exception e){ 
 			throw new Exception("数据库操作失败");
 		}finally{
 			db.close();
 		}		
	}
 	public  void  del(String table,String selection,String[] selectionArgs ) throws Exception{ 		
 		try{
 			dbHelper = new DatabaseHelper(context);
 			db = dbHelper.getWritableDatabase(); 			
 			db.delete(table, selection, selectionArgs); 			
 		}catch(Exception e){ 
 			throw new Exception("数据库操作失败");
 		}finally{
 			db.close();
 		}		
	}
 	
 	

 	
 	
 	public Cursor query(String table, String[] columns,String selection,String[] selectionArgs ,String groupBy,String having,String order,String limit){
 		dbHelper = new DatabaseHelper(context);
		db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, order, limit);
 		return cursor;
 	}
 	
 	public void closeDb(Cursor cursor){
 		if(cursor!=null){
 			cursor.close();
 			cursor=null;
 		}
 		db.close();
 	}
 	
	
}
